package com.rrx.mapper;

import com.rrx.model.Order;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/8/15.
 */
@Mapper
public interface OrderMapper {
    /**
     * 添加订单
     * @param order
     * @return
     */
    @Insert("insert into orders(number,userid,name,addressname,type,content,time) values(#{o.number},#{o.userId},#{o.name},#{o.addressname},#{o.type},#{o.content},now())")
   //@Insert("insert into orders(number,userid,addressname,type,content,time) values(123,123,'四川成都','电脑','显示坏了',0)")
    public int addOrder(@Param("o") Order order);
    /**
     *查询单个订单
     * @param number
     * @return
     */
    @Select("select id,number,userid,fettlerid,addressname,type,content,taketime,time,completedtime,status from orders where number=#{number}")
     @Results({
            @Result(id=true, property = "id",column ="id"),
            @Result(property = "number",column = "number"),
            @Result(property = "userId",column = "userid"),
            @Result(property = "fettlerId",column = "fettlerid"),
             @Result(property = "addressname",column = "addressname"),
             @Result(property = "type",column = "type"),
             @Result(property = "content",column = "content"),
             @Result(property = "taketime",column = "taketime"),
             @Result(property = "time",column = "time"),
             @Result(property = "completedtime",column = "completedtime"),
             @Result(property = "status",column = "status"),
    })
    public Order selectOrder(@Param("number") String number);

    /**
     * 查询所有未接单订单信息
     * @return
     */
    @Select("select u.phone,o.id as orderid ,name,number,userid,addressname,type,content from orders o,users u where fettlerid=-1 and status=0 and o.userid=u.id ")
    @Results({
            @Result(id=true, property = "id",column ="orderid"),
            @Result( property = "name",column ="name"),
            @Result( property = "phone",column ="phone"),
            @Result( property = "userId",column ="userid"),
            @Result(property = "number",column = "number"),
            @Result(property = "addressname",column = "addressname"),
            @Result(property = "type",column = "type"),
            @Result(property = "content",column = "content"),
    })
    public List<Order> selectAllOrede();

    /**
     * 师傅接单
     * @param fettlerid
     * @param orderid
     * @return
     */
    @Update("update orders set fettlerid=#{fettlerid},taketime=now(),status=1 where number=#{orderid}")
    public int updataFettlerId(@Param("fettlerid") int fettlerid, @Param("orderid") String orderid);
    /**
     * 用户确认完成订单
     */
    @Update("update orders set completedtime=now(),status=2 where number=#{orderid}")
    public int updataComplete(@Param("orderid") String orderid);
    /**
     * 用户ID查询所有订单
     */
    @Select("select id,number,userid,addressname,type,content,fettlerid,status from orders where userid=#{userid}")
    @Results({
            @Result(id=true, property = "id",column ="id"),
            @Result( property = "userId",column ="userid"),
            @Result(property = "number",column = "number"),
            @Result(property = "addressname",column = "addressname"),
            @Result(property = "type",column = "type"),
            @Result(property = "content",column = "content"),
            @Result(property = "fettlerid",column = "fettlerid"),
            @Result(property = "status",column = "status")
    })
    public List<Order> selectAllOredeByUserId(@Param("userid") int userid);

    /**
     * 分页获得订单列表
     * @param page   页数
     * @return
     */
    @Select("<script>" +
            "select o.id as id, o.number as number, o.name as name, f.name as fettlerName, o.addressname, concat(o.type,' ',o.content) as content, o.taketime as taketime, o.time as time, o.completedtime as completedtime, o.status as status " +
            "from orders o, fettler f " +
            "where f.id = o.fettlerid " +
            "<if test='type != -1'>" +
            " and o.status = #{type} " +
            "</if>" +
            " limit #{page}, 10" +
            "</script>")
    public Order[] getOrdersLimited(@Param("page") int page, @Param("type") int type);

    @Select("select o.id as id, concat(o.type,' ',o.content) as type, o.name as name, o.phone as phone, o.addressname as address from orders o where o.userid = #{userid} and status = #{status}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "type", property = "type"),
            @Result(column = "name", property = "name"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "address", property = "addressname")
    })
    public Order[] getOrdersByStatus(@Param("userid") int userid, @Param("status") int status);
}
